<template>
	<el-dialog :title="form.devId ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="区域" prop="areaId">
						<el-select v-model="form.areaId" placeholder="请选择区域" disabled>
							<el-option v-for="(item, index) in areaSelectData" :key="index" :label="item.name" :value="item.areaId" />
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="hass创建时间" prop="createdAt">
						<el-input v-model="form.createdAt" placeholder="请输入hass创建时间" disabled />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="hass禁用情况" prop="disabledBy">
						<el-input v-model="form.disabledBy" placeholder="请输入hass禁用情况" disabled />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="类型" prop="entryType">
						<el-input v-model="form.entryType" placeholder="请输入类型" disabled />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="制造商" prop="manufacturer">
						<el-input v-model="form.manufacturer" placeholder="请输入制造商" disabled />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="型号" prop="model">
						<el-input v-model="form.model" placeholder="请输入型号" disabled />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="hass更新时间" prop="modifiedAt">
						<el-input v-model="form.modifiedAt" placeholder="请输入hass更新时间" disabled />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="命名人" prop="nameByUser">
						<el-input v-model="form.nameByUser" placeholder="请输入命名人" disabled />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="名称" prop="customName">
						<el-input v-model="form.customName" placeholder="请输入名称" />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="软件版本" prop="swVersion">
						<el-input v-model="form.swVersion" placeholder="请输入软件版本" disabled />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="是否显示" prop="visible">
						<el-switch v-model="form.visible" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取 消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确 认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="HassDeviceRegistryDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj, validateExist } from '/@/api/homecenter/hassDeviceRegistry';
import { fetchSelectList } from '/@/api/homecenter/hassAreaRegistry';
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
// 定义字典

// 提交表单数据
const form = reactive({
	devId: '',
	areaId: '',
	createdAt: '',
	disabledBy: '',
	entryType: '',
	manufacturer: '',
	model: '',
	modifiedAt: '',
	nameByUser: '',
	customName: '',
	swVersion: '',
	visible: false,
});

const areaSelectData = ref([]) as any;

// 定义校验规则
const dataRules = ref({
	areaId: [{ required: true, message: '区域不能为空', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.devId = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取hassDeviceRegistry信息
	if (id) {
		form.devId = id;
		getHassDeviceRegistryData(id);
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.devId ? await putObj(form) : await addObj(form);
		useMessage().success(form.devId ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getHassDeviceRegistryData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj({ devId: id })
		.then((res: any) => {
			Object.assign(form, res.data[0]);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});

fetchSelectList().then((res: any) => {
	res.data.forEach((item) => {
		areaSelectData.value.push(item);
	});
});
</script>
